﻿<!DOCTYPE html>
<html>
<head>
<!--
  Copyright (c) 2015-2019 Jean-Marc VIGLINO, 
  released under CeCILL-B (french BSD like) licence: http://www.cecill.info/
-->
  <title>ol-ext: Flow style</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  
  <meta name="description" content="an OL3 stroke style with a set of cartographic patterns to use in your maps." />
  <meta name="keywords" content="ol, openlayers, vector, style, stroke, width, color, variable, sankey, flow" />

  <link rel="stylesheet" href="../style.css" />

  <!-- jQuery -->
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
  <!-- FontAwesome -->
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

  <!-- Openlayers -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css" />
  <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>

  
  <!-- ol-ext -->
  <link rel="stylesheet" href="../../dist/ol-ext.css" />
  <script type="text/javascript" src="../../dist/ol-ext.js"></script>
  <!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
  <script src="https://unpkg.com/elm-pep"></script>

  <style>
    .options label {
      display: inline-block;
      text-align: right;
      min-width: 4em;
    }
    .options input[type="number"] {
        width: 5em;
    }
  </style>
</head>
<body >
  <a href="https://github.com/Viglino/ol-ext" class="icss-github-corner"><i></i></a>

  <a href="../../index.html">
    <h1>ol-ext: Flow line style</h1>
  </a>
  <div class="info">
    <p>
      The <i>ol.style.FlowLine</i> is a line style to draw LineString with variable colors and widths.
      <br/>
      This can be used to display flows or <a href="https://en.wikipedia.org/wiki/Sankey_diagram">Sankey diagram</a> on a map.
    </p>
    <p>
      This example show how to set a width and color function to control the line drawing.
      <br/>
      Look at <a href='map.style.flowline.html'>this example</a> for more options.
    </p>
  </div>

  <!-- Map div -->
  <div id="map" style="width:600px; height:400px;"></div>

  <div class="options" style="min-width:300px;">
    <h2>Options:</h2>
    <ul>
			<li>
				<label>
					<input type="radio" name="op" checked="checked" value="drawLine" /> 
					Draw
				</label>
				<label>
					<input type="radio" name="op" value="modify" /> 
					Modify
				</label>
			</li>
		</ul>
  </div>
  
<script type="text/javascript">
  
  // Layers
  var layer = new ol.layer.Geoportail({ layer: 'ORTHOIMAGERY.ORTHOPHOTOS' });

  // The map
  var map = new ol.Map({
    target: 'map',
    view: new ol.View ({
      zoom: 5,
      center: [166326, 5992663]
    }),
    layers: [layer]
  });

  // Default style
  var defaultStyle = new ol.style.Style({
    image: new ol.style.Circle({
      radius: 6,
      fill: new ol.style.Fill({ color: [0, 153, 255, 1] }),
      stroke: new ol.style.Stroke({ color: [255, 255, 255, 1], width: 1.5 })
    })
  });
  // Flow style
  var flowStyle = new ol.style.FlowLine({
    lineCap: 'round',
    color: function(f,step){
      if (step<.5) return [255,255*step*2,0];
      else return [255,255-255*(step-.5)*2,0];
    },
    width: function(f, step) { 
      return 30*Math.sin(Math.PI*step); 
    }
  });
  function getStyle(feature, res) {
    return [ defaultStyle, flowStyle ];
  }

  // Nouvelle source de donnee
  var vector = new ol.layer.VectorImage({
    source: new ol.source.Vector({ features: new ol.Collection() }),
    style: getStyle
  })
  map.addLayer(vector);
  vector.getSource().addFeature(new ol.Feature(new ol.geom.LineString([[259274, 6398696], [63595, 5958419], [635956, 5772524]])));

  // modify and draw interaction
  var interactions = {
    modify: new ol.interaction.Modify ({ features: vector.getSource().getFeaturesCollection() }),
    drawLine: new ol.interaction.Draw({
      type: 'LineString',
      style: getStyle,
      source: vector.getSource()
    })
  }
  for (var i in interactions) map.addInteraction(interactions[i]);
  setInteraction = function () {
    var name = $('[name="op"]:checked').val();
    for (var i in interactions) {
      interactions[i].set("active", (i==name));
    }
  }
  setInteraction();
  $("input:radio").on("change",setInteraction);
</script>

</body>
</html>